var prefix = jsctx + "/parking";

var addParkingGate = (function () {
    function init() {
        validateRule();
    };

    var isajaxsend = true;
    var saved = false;
    function save() {
        if (!saved) {
            var formData = new FormData($("#frmParkingGate")[0]);
            $.ajax({
                cache: true,
                type: "POST",
                url: prefix + "/saveParkingGate",
                data: formData,
                processData: false,
                contentType: false,
                async: false,
                beforeSend: function () {
                    saved = true
                },
                error: function (request) {
                    parent.layer.alert("Connection error");
                },
                success: function (data) {
                    if (data.status == 200) {
                        parent.layer.msg("操作成功");
                        window.history.back();
                    } else {
                        parent.layer.alert(data.message)
                    }

                },
                complete: function () {
                    saved = true
                }
            });
        }
    }

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#frmParkingGate").validate({
            rules: {
                elecStationId: {
                    required: true
                },
                parkingType: {
                    required: true
                },
                gateType: {
                    required: true
                },
                gateCode: {
                    required: true
                },
                gateName: {
                    required: true
                },
            },
            messages: {
                elecStationId: {
                    required: icon + "请选择站点"
                },
                parkingType: {
                    required: icon + "请选择道闸类型"
                },
                gateType: {
                    required: icon + "请选择出入口类型"
                },
                gateCode: {
                    required: icon + "请输入道闸编号"
                },
                gateName: {
                    required: icon + "请选择道闸名称"
                },

            },
            submitHandler: function (form) {
                save();
            }
        })
    }
    return {
        init: init,
        searchStation:searchStation
    };
    

    function searchStation() {
        var searchStation = $("#searchStation").val();
        var tenantCode = $("#tenantCode").val();
        $.ajax({
            cache: true,
            type: "GET",
            url: prefix + "/searchStation",
            data: {
                "stationName":searchStation,
                "tenantCode":tenantCode
            },
            async: false,
            beforeSend: function () {
                isajaxsend = false
            },
            error: function (request) {
                parent.layer.alert("Connection error");
            },
            success: function (data) {
                $("#elecStationId").html("");
                $('#elecStationId').append("<option value='' selected></option>");
                for (var i = 0; i < data.length; i++){
                    $('#elecStationId').append("<option value='" + data[i].elecStationId +"'>" + data[i].stationName + "</option>");
                }
            },
            complete: function () {
                isajaxsend = false
            }
        });

    }
})();
